<template>
    <div class="center_div">
        <el-tabs v-model="activeName" style="width: 100%;margin-top: 20px">
            <el-tab-pane  name="添加会议室"  label="添加会议室">
                <el-form ref="form" :model="roomDetail" label-width="100px" style="margin-top:50px;">
                    <el-form-item label="会议室编号： " prop="roomnum">
                        <el-input v-model="roomDetail.roomnum" style="width: 50%;"></el-input>
                    </el-form-item>
                    <el-form-item label="会议室名称： " prop="roomname">
                        <el-input v-model="roomDetail.roomname" style="width: 50%;"></el-input>
                    </el-form-item>
                    <el-form-item label="会议室容量： " prop="capacity">
                        <el-input-number v-model="roomDetail.capacity" controls-position="right" :min="1" :max="100"></el-input-number>
                    </el-form-item>
                    <el-form-item label="会议室描述： " prop="description">
                        <el-input type="textarea" placeholder="请填写使用原因，越详细审批通过率越高哦" v-model="roomDetail.description" style="width: 50%;"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">立即添加</el-button>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane  name="修改会议室"  label="修改会议室">
                <el-table :data="roomList" style="margin-top: 10px">
                    <el-table-column prop="roomnum" label="会议室编号" min-width="90" align="center">
                        <template slot-scope="scope">
                            <el-tag type="primary">{{scope.row.roomnum}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="roomname" label="会议室名" min-width="90"></el-table-column>
                    <el-table-column prop="capacity" label="容量" min-width="90" align="center"></el-table-column>
                    <el-table-column label="会议室状态" min-width="90"  align="center">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status === 1 " type="warning">维修中</el-tag>
                            <el-tag v-if="scope.row.status === 0 " type="success">正常</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="description" label="描述" min-width="120" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column label="操作" min-width="150" align="center">
                        <template slot-scope="scope">
                            <el-button @click="editMeeting(scope.row)">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <el-dialog
                        title="编辑会议室信息"
                        :visible.sync="dialogVisible"
                        width="50%">
                    <el-form ref="form" :model="meetingDetail" label-width="100px" style="margin-top:50px;">
                        <el-form-item label="编号" prop="roomnum">
                            <el-input v-model="meetingDetail.roomnum" disabled style="width: 50%;"></el-input>
                        </el-form-item>
                        <el-form-item label="会议室名称" prop="roomname">
                            <el-input v-model="meetingDetail.roomname" style="width: 50%;"></el-input>
                        </el-form-item>
                        <el-form-item label="会议室容量" prop="capacity">
                            <el-input v-model="meetingDetail.capacity" style="width: 50%;"></el-input>
                        </el-form-item>
                        <el-form-item label="会议室状态" prop="status">
                            <el-radio-group v-model="meetingDetail.status">
                                <el-radio :label="1">维修中</el-radio>
                                <el-radio :label="0">正  常</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="描述" prop="description">
                            <el-input v-model="meetingDetail.description" style="width: 50%;"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="dialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="updateRoom">更新</el-button>
                      </span>
                </el-dialog>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
    export default {
        name: "add-meeting",
        data() {
            return {
                activeName: '添加会议室',
                roomDetail: {roomnum: 0, roomname: '', capacity: 1, status: 0, description: ''},
                meetingDetail: {},
                roomList: [],
                dialogVisible: false
            }
        },
        created() {
            this.fetchMeetingList()
        },
        methods: {
            onSubmit() {
                if (this.roomDetail.roomnum === 0 || this.roomDetail.capacity === 0) {
                    this.$message.error('存在参数未填写')
                    return
                }
                if (this.roomDetail.roomname.length === 0 || this.roomDetail.description.length === 0) {
                    this.$message.error('存在参数未填写')
                    return
                }
                console.log(this.roomDetail)
                this.addRoom()
            },
            async addRoom(){
              let {code ,data ,msg} = await this.$post('/doaddmr',
                      {data: {meetingRoom:{
                                  "roomnum":this.roomDetail.roomnum,
                                  "roomname":this.roomDetail.roomname,
                                  "capacity":this.roomDetail.capacity,
                                  "status":0,
                                  "description": this.roomDetail.description
                              }}})
              if(code === 200) {
                console.log(data)
                this.$message({
                  message: '添加会议室成功',
                  type: 'success'
                })
                this.meetingDetail = {}
                this.fetchMeetingList()
                this.dialogVisible = false
              }else {
                this.$message.error(msg)
              }
            },
            async fetchMeetingList(){
                let {code ,data ,msg} = await this.$post('/meetingrooms')
                if(code === 200) {
                    this.roomList = data
                }else {
                    this.$message.error(msg)
                }
            },
            editMeeting(row) {
                this.meetingDetail = row
                console.log(this.meetingDetail)
                this.dialogVisible = true
            },
            async updateRoom() {
                let {code ,data ,msg} = await this.$post('/updateroom',
                    {data: {meetingRoom:{
                                "roomid": this.meetingDetail.roomid,
                                "roomnum":this.meetingDetail.roomnum,
                                "roomname":this.meetingDetail.roomname,
                                "capacity":this.meetingDetail.capacity,
                                "status":this.meetingDetail.status,
                                "description": this.meetingDetail.description
                            }}})
                if(code === 200) {
                    console.log(data)
                    this.$message({
                        message: '更新会议室成功',
                        type: 'success'
                    })
                    this.meetingDetail = {}
                    this.fetchMeetingList()
                    this.dialogVisible = false
                }else {
                    this.$message.error(msg)
                }
            },
            delRoom(id) {
                console.log(id)
            }
        }
    }
</script>

<style>
    .center_div{
        width: 60%;
        margin-left:auto;
        margin-bottom: 50px;
        margin-top: 50px;
    }
</style>